<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class RedisStore extends Model
{
    public $redis; //redis实例

    public function init()
    {
        //默认redis实例
        $this->redis = Yii::$app->redis;
    }

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
    }

    /**
     * 设置实际使用的redis实例
     * @param $redis
     */
    public function setRedis($redis)
    {
        $this->redis = $redis;
    }

    /**
     * @return array customized attribute labels
     */
    public function attributeLabels()
    {
    }

    public function getKey($str)
    {
        return md5($str);
    }
    
    /**
     * 微信发消息 插入队列  可以当做通用的队列来使用
     * @param $data
     * @return mixed
     */
    public function setQueue( $key,$data )
    {
        if($data){
            $redis = Yii::$app->redis;
            return  $redis->LPUSH($key, $data);
        }else{
            return false;
        }
    }

    /**
     * 微信收消息 取队列  可以当做通用的队列来使用
     * @param $data
     * @return mixed
     */
    public function getQueue( $key )
    {
        if($key){
            $redis = Yii::$app->redis;
            return  $redis->LPOP($key);
        }else{
            return false;
        }
    }
    
    /**
     * 存储微信的用户列表
     * @param $data
     * @param $key
     * @return mixed
     */
    public function setWechatuserlist( $key,$field,$value )
    {
        if($key&&$field&&$value){
            $redis = Yii::$app->redis;
            return  $redis->hset($key,$field,$value);
        }else{
            return false;
        }
    }
    
    /**
     * 删除微信列表的key
     * @param $data
     * @param $key
     * @return mixed
     */
    public function delWechatuserlist( $key )
    {
        if( $key ){
            $redis = Yii::$app->redis;
            return  $redis->del($key);
        }else{
            return false;
        }
    }
    
    /**
     * 微信列表的key 是否存在
     * @param $key
     * @return mixed
     */
    public function eXISTSWechatuserlist( $key )
    {
        if( $key ){
            $redis = Yii::$app->redis;
            return  $redis->exists($key);
        }else{
            return false;
        }
    }
    
    /**
     * 微信列表的值
     * @param $data
     * @param $key
     * @return mixed
     */
    public function getWechatuserlist( $key,$field )
    {
        if( $key ){
            $redis = Yii::$app->redis;
            return  $redis->hget($key,$field);
        }else{
            return false;
        }
    }

    /**
     * 先进先出队列
     * @param $key
     * @param $value
     */
    public function pushQueue($key, $value)
    {
        $redis = Yii::$app->redisCache2;
        return $redis->LPUSH($key, $value);
    }

    /**
     * 先进先出队列
     * @param $key
     */
    public function popQueue($key)
    {
        $redis = Yii::$app->redisCache2;
        return $redis->RPOP($key);
    }

    /**
     * 先进先出队列
     * @param $key
     * @param $value
     */
    public function pushQueueNew($key, $value)
    {
        return $this->redis->LPUSH($key, $value);
    }

    /**
     * 先进先出队列
     * @param $key
     */
    public function popQueueNew($key)
    {
        return $this->redis->RPOP($key);
    }

    public function hincrBy($key, $field, $num)
    {
        return $this->redis->executeCommand('HINCRBY', [$key, $field, $num]);
    }

    public function hget($key, $field)
    {
        return intval($this->redis->HGET($key, $field));
    }

    public function hmget($key, $fields)
    {
        array_unshift($fields, $key);
        return $this->redis->executeCommand('HMGET', $fields);
    }

    /**
     * @param $values, ['test_collection', 'key1', 'val1', 'key2', 'val2']
     * @return mixed
     */
    public function hmset($values)
    {
        return $this->redis->executeCommand('HMSET', $values);
    }

    /**
     * @param $values, ['test_collection', 'key1', 'val1', 'key2', 'val2']
     * @return mixed
     */
    public function hsetnx($values)
    {
        return $this->redis->executeCommand('HSETNX', $values);
    }

    /**
     * 设置缓存
     * @param $key
     * @param $expire
     * @param $value
     * @return mixed
     */
    public function setex($key, $expire, $value)
    {
        $redis = Yii::$app->redis;

        return $redis->setex($key, $expire, $value);
    }

    /**
     * @param $key
     * @return mixed
     */
    public function get($key)
    {
        $redis = Yii::$app->redis;

        return $redis->get($key);
    }

    /**
     * @param $key
     * @param $value
     * @return mixed
     */
    public function set($key, $value)
    {
        $redis = Yii::$app->redis;

        return $redis->set($key, $value);
    }

    /**
     * 原子增加计数
     * @param $key
     * @param int $num
     * @return mixed
     */
    public function incrBy($key, $num = 1)
    {
        return $this->redis->executeCommand('INCRBY', [$key, $num]);
    }

    /**
     * 根据key获取值
     */
    public function getByKey($key)
    {
        return $this->redis->get($key);
    }

    /**
     * 清除此key值下的数据
     */
    public function clearRedisKey($key)
    {
        return $this->redis->del($key);
    }
}
